package com.guge.sports.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guge.sports.entity.Areas;
import com.guge.sports.entity.Dto.Option;
import com.guge.sports.entity.Dto.StSchoolDto;
import com.guge.sports.entity.Dto.SchoolRunDetailDto;
import com.guge.sports.entity.StSchool;
import com.guge.sports.entity.StTeacher;
import com.guge.sports.mapper.AreasMapper;
import com.guge.sports.mapper.StSchoolMapper;
import com.guge.sports.service.StSchoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guge.sports.toolPackage.base.BaseService;
import com.guge.sports.toolPackage.utils.MyPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-09-14
 */
@Service
public class StSchoolServiceImpl extends BaseService<StSchool> implements StSchoolService {

    @Autowired
    private StSchoolMapper stSchoolMapper;

    @Autowired
    private AreasMapper areasMapper;


    @Override
    public List<Option> getSchoolNameAndDidList() {
        List<StSchool> stSchools = stSchoolMapper.selectList(null);
        List<Option> collect = stSchools.stream().map(item -> {
            Option option = new Option();
            option.setValue(String.valueOf(item.getScid()));
            option.setLabel(item.getSchoolName());
            return option;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public MyPager<StSchoolDto> getSchoolMessageList(MyPager<StSchoolDto> myPager, StSchoolDto stSchoolDto) {
        Integer count = stSchoolMapper.selectCountSchoolPage(stSchoolDto);
        myPager.setTotalCount(count);
        List<StSchoolDto> list = stSchoolMapper.getSchoolMessageList(myPager, stSchoolDto);
        myPager.setList(list);
        return myPager;
    }

    @Override
    public void AddSchool(StSchoolDto stSchoolDto) {
        Date date = new Date();
        StSchool stSchool = new StSchool();
        stSchool.setSchoolName(stSchoolDto.getSchoolName());
        stSchool.setSort(stSchoolDto.getSort());
        stSchool.setState(stSchoolDto.getState());
        stSchool.setRemarks(stSchoolDto.getRemarks());
        stSchool.setCreateTime(date);
        stSchool.setStateTime(date);
        stSchool.setUpdateTime(date);

        stSchoolMapper.insert(stSchool);
    }

    @Override
    public void DeleteSchool(Long id) {
        StSchool stSchool = new StSchool();
        stSchool.setScid(id);
        stSchool.setIsdelete(1);
        stSchoolMapper.updateById(stSchool);
    }

    @Override
    public StSchoolDto SelectById(Long id) {
        return stSchoolMapper.selectById(id);

    }

    @Override
    public void UpdateById(StSchoolDto stSchoolDto) {
        System.out.println(stSchoolDto);
        LambdaQueryWrapper<Areas> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Areas::getQxname, stSchoolDto.getQxname());
        Areas areas = areasMapper.selectOne(wrapper);
        System.out.println(areas);
        stSchoolDto.setAreaId(areas.getId());

        stSchoolMapper.updateById(stSchoolDto);
    }

    @Override
    public List<Option> SelectArearsList() {
        List<Areas> list = areasMapper.selectList(null);
        List<Option> collect = list.stream().map(item -> {
            Option option = new Option();
            option.setValue(String.valueOf(item.getId()));
            option.setLabel(item.getQxname());
            return option;
        }).collect(Collectors.toList());

        return collect;
    }
}
